﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Automobile.service.Common;
using Automobile.service.Model;
using System.Data;
using System.Data.SqlClient;

namespace Automobile.service.DAL
{
    public class EmployeeDAL
    {
        SQLHelper sqlhelper = new SQLHelper();
        /// <summary>
        /// 员工管理——添加
        /// </summary>
        /// <param name="emp"></param>
        /// <returns></returns>
        public int AddEmployee(Employee emp)
        {
            SqlParameter[] addemp ={
                                     new SqlParameter("@empname",emp.Employee_Name),
                                     new SqlParameter("@emppwd",emp.Employee_Pwd),
                                     new SqlParameter("@empphone",emp.Employee_Phone),
                                     new SqlParameter("@empaddr",emp.Employee_Addr),
                                     new SqlParameter("@empremark",emp.Employee_Remark)
                                 };
            return sqlhelper.ExecNonquery_Proc("AddEmployee", addemp);
        }
        
        /// <summary>
        /// 员工管理——分页显示
        /// </summary>
        /// <param name="PageIndex"></param>
        /// <param name="PageSize"></param>
        /// <returns></returns>
        public List<Employee> ShowlistEmployee(int PageIndex, int PageSize)
        {
            SqlParameter[] GetlistEmployee ={
                                     new SqlParameter("@PageIndex",PageIndex),
                                     new SqlParameter("@PageSize",PageSize),
                                 };
            DataTable dt = sqlhelper.ExecDataTable_Proc("ShowListEmployee", GetlistEmployee);
            List<Employee> list = new List<Employee>();
            foreach (DataRow item in dt.Rows)
            {
                Employee emp = new Employee();
                emp.rowempnum = Convert.ToInt32(item["rowempnum"]);
                emp.Employee_Id = Convert.ToInt32(item["Employee_Id"]);
                emp.Employee_Name = item["Employee_Name"].ToString();
                emp.Employee_Pwd = item["Employee_Pwd"].ToString();
                emp.Employee_Phone = item["Employee_Phone"].ToString();
                emp.Employee_Addr = item["Employee_Addr"].ToString();
                emp.Employee_State = Convert.ToInt32(item["Employee_State"]);
                emp.Employee_Remark = item["Employee_Remark"].ToString();
                list.Add(emp);
            }
            return list;
        }
        /// <summary>
        /// 员工管理——启用
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public int Updempstatetrue(int id)
        {
            SqlParameter[] empstate_true ={
                                     new SqlParameter("@empid",id),
                                 };
            return sqlhelper.ExecNonquery_Proc("updempstate_true", empstate_true);
        }
        
        /// <summary>
        /// 员工管理——停用
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public int Updempstatefalse(int id)
        {
            SqlParameter[] empstate_false ={
                                     new SqlParameter("@empid",id),
                                 };
            return sqlhelper.ExecNonquery_Proc("updempstate_false", empstate_false);
        }
        
        /// <summary>
        /// 员工管理——获取要修改的数据
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public DataTable GetEmployeeByempid(int id)
        {
            SqlParameter[] GetempByid ={
                                           new SqlParameter("@empid",id)
                                       };
            return sqlhelper.ExecDataTable_Proc("GetEmployeeByEmpid", GetempByid);
        }
        /// <summary>
        /// 员工管理——修改员工信息
        /// </summary>
        /// <param name="emp"></param>
        /// <returns></returns>
        public int UpdEmployeeByempid(Employee emp)
        {
            SqlParameter[] updemp ={
                                     new SqlParameter("@empid",emp.Employee_Id),
                                     new SqlParameter("@empname",emp.Employee_Name),
                                     new SqlParameter("@emppwd",emp.Employee_Pwd),
                                     new SqlParameter("@empphone",emp.Employee_Phone),
                                     new SqlParameter("@empaddr",emp.Employee_Addr),
                                     new SqlParameter("@empremark",emp.Employee_Remark)
                                 };
            return sqlhelper.ExecNonquery_Proc("UpdEmployee", updemp);
        }
        
        /// <summary>
        /// 登陆
        /// </summary>
        /// <param name="empname"></param>
        /// <param name="emppwd"></param>
        /// <returns></returns>
        public string Landing(string empname, string emppwd)
        {
            string sql = "select employee_name,employee_pwd,employee_state from employee where employee_name='" + empname + "'";
            DataTable dtname = sqlhelper.ExecDataTable(sql);
            if (dtname.Rows.Count > 0)
            {
                sql += " and employee_pwd='" + emppwd + "'";
                DataTable dtpwd = sqlhelper.ExecDataTable(sql);
                if (dtpwd.Rows.Count > 0)
                {
                    if (Convert.ToInt32(dtpwd.Rows[0][2]) == 0)
                    {
                        return "登录成功";
                    }
                    else
                    {
                        return "用户未启用";
                    }
                }
                else
                {
                    return "密码错误";
                }
            }
            else
            {
                return "用户名不存在";
            }
        }
        /// <summary>
        /// 获取登陆人的编号
        /// </summary>
        /// <param name="empname"></param>
        /// <param name="emppwd"></param>
        /// <returns></returns>
        public int GetEmpidByLanding(string empname,string emppwd)
        {
            string sql = "select Employee_Id from employee where employee_name='" + empname + "' and employee_pwd='" + emppwd + "'";
            return Convert.ToInt32(sqlhelper.ExecScalar(sql));
        }
        /// <summary>
        /// 员工管理——总记录数
        /// </summary>
        /// <returns></returns>
        public int EmpCount()
        {
            return Convert.ToInt32(sqlhelper.ExecScalar_Proc("EmpCount"));
        }
        /// <summary>
        /// 员工管理——根据编号获取状态
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public int GetEmpStateById(int id)
        {
            string sql = "select Employee_State from Employee where Employee_Id=" + id;
            return Convert.ToInt32(sqlhelper.ExecScalar(sql));
        }
        /// <summary>
        /// 员工管理——根据用户名查询
        /// </summary>
        /// <param name="empname"></param>
        /// <returns></returns>
        public DataTable GetEmpByEmpName(string empname,int pageIndex,int pagesize)
        {
            string Emp_Name = '%' + empname + '%';
            SqlParameter[] Getemp ={
                                     new SqlParameter("@empname",Emp_Name),
                                     new SqlParameter("@PageIndex",pageIndex),
                                     new SqlParameter("@PageSize",pagesize)
                                 };
            return sqlhelper.ExecDataTable_Proc("GetListEmployeeByName", Getemp);
        }
    }
}
